package com.xinyiweidai.xinyiweidai.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.AuthCodeBean;
import com.xinyiweidai.xinyiweidai.bean.LoginBean;
import com.xinyiweidai.xinyiweidai.util.Api;
import com.xinyiweidai.xinyiweidai.util.ImgCodeUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;


/**
 * class:   ResetPasswordActivity 
 * data：   2018/9/10  14:33
 * author:  LanX
 * Description:  忘记密码
 * 
 */
public class ResetPasswordActivity extends BaseActivity {


    private EditText et_reset_number;
    private EditText et_reset_authCode;
    private Button btn_auth_code;
    private EditText et_reset_password;
    private Button btn_reset;
    private String mobile;
    
    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_password;
    }

    
    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back,"重置密码",0,true);
    }

    
    @Override
    protected void initView(Bundle savedInstanceState) {
        et_reset_number = (EditText) findViewById(R.id.et_reset_number);
        et_reset_authCode = (EditText) findViewById(R.id.et_reset_authCode);
        et_reset_password = (EditText) findViewById(R.id.et_reset_password);
        btn_auth_code = (Button) findViewById(R.id.btn_auth_code);
        btn_reset = (Button) findViewById(R.id.btn_reset);
    }


    @Override
    protected void addClickEvent() {
        btn_reset.setOnClickListener(view -> checkReset());
        btn_auth_code.setOnClickListener(view -> sendAuthCode());
    }

    
    //判断手机号 发送请求 开始倒计时 设置短信验证码
    private void sendAuthCode() {
        mobile=et_reset_number.getText().toString().trim();
        if (mobile.length()!=11) {
            MyToast.showError(this, "号码错误");
            return;
        }

        Map<String,String> map=new HashMap<>();
        map.put("mobile",mobile);
        map.put("type","wjmm");

        sendRequest(Api.authCode,map,1);
    }

    
    private void checkReset() {
         mobile = et_reset_number.getText().toString();
        String authCode = et_reset_authCode.getText().toString();
        String password = et_reset_password.getText().toString();

        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(authCode) || TextUtils.isEmpty(password)) {
            MyToast.showError(this, "输入有误");
            return;
        }

        Map<String,String> map=new HashMap<>();
        map.put("mobile",mobile);
        map.put("newpassword",password);
        map.put("captcha", authCode);
        L.e(map.toString());
        sendRequest(Api.reset,map,2);
    }


    private void sendRequest(String url,Map<String,String> map,int requestType) {
        showLoading("正在加载");
        OkHttpUtils.get().url(url).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                MyToast.showError(ResetPasswordActivity.this,"网络异常");
                dismissLoading();
            }

            @Override
            public void onResponse(String response) {
                L.e(response);
                dismissLoading();
                parseData(response,requestType);
            }
        });
    }


    //requestType  1是发送验证码    2是完成注册
    private void parseData(String response, int requestType) {
        if (requestType==1){
            AuthCodeBean bean=new Gson().fromJson(response,AuthCodeBean.class);
            if (bean.getCode()==1){
                MyToast.showSuccess(ResetPasswordActivity.this,"发送成功");
                startCountDown();
            }else{
                MyToast.showError(ResetPasswordActivity.this,bean.getMsg());
            }
        }else{
            LoginBean bean=new Gson().fromJson(response,LoginBean.class);
            if (bean.getCode()==1){
                MyToast.showSuccess(ResetPasswordActivity.this,"重置成功");
                Intent intent = new Intent();
                intent.putExtra("mobile", bean.getData().getUserinfo().getMobile());
                setResult(RESULT_OK,intent);
                finish();
            }else{
                MyToast.showError(ResetPasswordActivity.this,bean.getMsg());
            }
        }
    }


    //验证码倒计时
    private void startCountDown() {
        new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                btn_auth_code.setEnabled(false);
                btn_auth_code.setText(String.valueOf(millisUntilFinished / 1000) + "秒后重试");
                btn_auth_code.setTextColor(getResources().getColor(R.color.white_115));
            }

            @Override
            public void onFinish() {
                btn_auth_code.setEnabled(true);
                btn_auth_code.setText("获取验证码");
                btn_auth_code.setTextColor(getResources().getColor(R.color.appColor));
            }
        }.start();
    }
}
